<?
include "../db_config.php";
include "Database.class.php";

$db = new Database($host_name, $user_name, $db_password, $db_name);
$db->connect();


$lat = $HTTP_GET_VARS['lat'];
$lng = $HTTP_GET_VARS['lng'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Korea Subway Chaehyun Lee</title>

<script
src="http://maps.google.com/maps?file=api&v=2&sensor=true&key=ABQIAAAAi-Dpl_qi3OFGJJzCfeNcbxSR9A0C6j8jFyzwLqb8htu1JtmEPBQAo3YZkFpuj6dhITMD9bWq-b0lXw"
type="text/javascript"></script>


<style type="text/css">
#waiting{
	position:absolute;
	top:300px;
	left:300px;
	visibility:hidden;
	background-color:white;
	z-index:100;
}
#totalNum{
	position:absolute;
	top:0px;
	left:0px;
}
#infoWin{
	position:absolute;
	top:0px;
	left:100px;
	background-color:white;
	margin:0px 10px 10px 10px;
	z-index:100;
}
#infoWin2{
	position:absolute;
	top:0px;
	left:400px;
	background-color:white;
	margin:0px 10px 10px 10px;
	z-index:100;
}

#refreshbtn{
	position:absolute;
	top:50px;
	left:500px;
	width:50px;
	height:50px;
	background-color:white;
	z-index:100;
}
.distance{
	background-color:white;
}


</style>

<script type="text/javascript">
var mapWidth = '100%';
var mapHeight = '100%';
var mapObj;
var center;
function initialize()
{
	if(GBrowserIsCompatible()){
		mapObj = new GMap2(document.getElementById("map_canvas"));
<?
if($lat != null && $lng != null)
	echo "center = new GLatLng($lat, $lng);\n";
else
	echo "center = new GLatLng(37.5225753993	,127.000369221);\n";
?>
		mapObj.setCenter(center, 14);
		//mapObj.setZoom(14);
		mapObj.setUIToDefault();

		getAPPoints();

		var i = 0;
		GEvent.addListener(mapObj, "click", getAPPoints);
		GEvent.addListener(mapObj, "dragend", getAPPoints);
		GEvent.addListener(mapObj, "zoomend", getAPPoints);

	}
	setInterval("refresh()", 1000);
}
function refresh()
{
	if(refreshState == 1)
		getAPPoints();
}

function addmarker(x, y, remain, line)
{
	var n1, n2;

	var pt_img = "http://ozs.kr/lbs/point.png";
	var base_img = "./img/";
	n1 = new GLatLng(x, y);

	var icon = new GIcon(G_DEFAULT_ICON);
	/*
	var updown_s = "상행";
	if(updown == 2)
		updown_s = "하행";
		*/
	//역 정차 상태
	if(remain <= -10)
	{
		/*
		var stop = new GIcon(G_DEFAULT_ICON);
		stop.image = base_img + "stop.png";
		stop.iconSize = new GSIze(40, 20);
		stop2 = {icon:stop};
		*/
		//mapObj.addOverlay(new GMarker(n1, stop2));

		icon = new GIcon(G_DEFAULT_ICON);
		icon.shadow = base_img + "sw"+line+".gif";
		icon.shadowSize = new GSize(40, 48);
		icon.image= base_img + "stop.png";
		icon.iconSize = new GSize(40, 20);
	}
	else if(remain < 0)
	{
		//출발전 카운트 상태
		var j = -1*remain;
		icon = new GIcon(G_DEFAULT_ICON);
		icon.shadow = base_img + "sw"+line+".gif";
		icon.shadowSize = new GSize(40, 48);
		icon.image= base_img + j + ".gif";
		icon.iconSize = new GSize(20, 20);
	}
	else
	{
		//움직이는 중
		icon = new GIcon(G_DEFAULT_ICON);
		icon.image = base_img + "sw"+line+".gif";
		icon.iconSize = new GSize(40, 48);
	}
	
//	var s = "남은 시간 : " + remain + "초\n"+line+"호선 "+updown_s;

//	markerOptions = {title:s, icon:icon};
	markerOptions = {icon:icon};
	mapObj.addOverlay(new GMarker(n1, markerOptions));
	return;

}
function showList2(list)
{
	document.getElementById("infoWin2").innerHTML = list;
}
function showList(list)
{
	document.getElementById("infoWin").innerHTML = list;
}
function hideList()
{
	//document.getElementById("infoWin").style.visibility = "hidden";
	document.getElementById("infoWin").innerHTML = "";
}

function getAPRawData(bssid)
{
	var xmlhttp = GXmlHttp.create();
	//xmlhttp.setType(0);
	var url = "getAPRawData.php?bssid="+bssid;
	xmlhttp.open('GET', url, true);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
			showAPRawData(xmlhttp.responseText);
	}
	xmlhttp.send(null);
}
	
function showAPRawData(res)
{
	//window.console.log(str);
	//window.console.log("showAPRawData");
	eval("var s = " + res +";");
	//eval(str);
	document.getElementById('totalNum').innerHTML = "총 AP : "+s.totalNum + "개";
	mapObj.clearOverlays();

	//boundary를 구하기 위해
	var bounds = new GLatLngBounds();

	for(var i in s.Points)
	{
		var node = s.Points[i];
		var p = new GLatLng(node.lat, node.lng);
		bounds.extend(p);
		addmarker(node.lat, node.lng, 0, node.SNR+"|"+node.Sig, 1);
	}
	//var b = line.getBound();
	//mapObj.setBound(b[0], b[1], b[2], b[3]);
	mapObj.setCenter(bounds.getCenter(),mapObj.getBoundsZoomLevel(bounds));

	document.getElementById('waiting').style.visibility = "hidden";

}

function showAPPoints(res)
{
	//window.console.log("showAPPoints()");
	eval("var s = " + res +";");
	document.getElementById('totalNum').innerHTML = "현재 지도에서 열차 수 : "+s.totalNum + "개";
	mapObj.clearOverlays();
	for(var i in s.Points)
	{
		var node = s.Points[i];
		//window.console.log(node);
		//window.console.log(node.line);
		addmarker(node.lat, node.lng, node.remainTime, node.line);
	}
	document.getElementById('waiting').style.visibility = "hidden";
}
//ruler 관련 코드
var rulerStatus = 0;
//최소 AP 개수
var minAPNum = 1;

var pt1, pt2;
function changeMinAPNum()
{
	var obj = document.getElementById('minAPNum');
	minAPNum = obj.value;

	getAPPoints();
}
function getAPPoints()
{
	//window.console.log("getAPPoints");
	var b = mapObj.getBounds();
	//ruler가 동작 중일 때는 동작 안함
	if(rulerStatus != 0)
		return;
	//var lt = mapObj.fromTM128ToLatLng(new NTM128(b[0], b[1]));
	//var rb = mapObj.fromTM128ToLatLng(new NTM128(b[2], b[3]));
	var lt = b.getSouthWest();
	var rb = b.getNorthEast();
	var obj = document.getElementById('waiting');
	//obj.style.visibility = "visible";
	var xmlhttp = GXmlHttp.create();
	var url = "getTrainInfo.php?lat2="+rb.lat()+"&lng1="+lt.lng();
	url = url + "&lat1="+lt.lat() +"&lng2="+rb.lng();
	url = url + "&updown=1";
	//var url = "getTrainInfo.php?";
	//window.console.log(url);
	xmlhttp.open('GET', url, true);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		//window.console.log(xmlhttp.responseText);
			showAPPoints(xmlhttp.responseText);
	}
	xmlhttp.send(null);
}
var logSelect = 1;
function loadLog()
{
	var obj = document.getElementById('log');
	logSelect = obj.value;
	getAPPoints();
}

var refreshState = 0;
function changeRefreshState()
{
	if(refreshState == 1)
		refreshState = 0;
	else
		refreshState = 1;
}
function check()
{
	var line = document.line;
	for(i = 0; i< line.hoho.length; i++)
	{
		if(line.hoho[i].checked == true)
		{
			//alert(chk[i].value);
			window.console.log(line.hoho[i].value);
		}
	}
}

</script>

</head>
<body onload="initialize()" onunload="GUnload()">
<div id=lineSelector>
<form name=line>
<?
function printLineInfo($line)
{
	echo "<td> $line 호선 상행
	<input type=checkbox name=hoho value=\"$line 호선상행\" checked
	onchange=\"check();\"><BR>\n";
//	echo "$line 호선 하행
//	<input type=checkbox name=line_2_$line value=\"$line호선하행\" > </td>\n";
echo "</td>";
}
$sql = "select distinct line from trainInfo";
$rows = $db->fetch_all_array($sql);
echo "<table style='font-size:9pt'>\n";
echo "<tr>\n";
foreach($rows as $data)
{
	printLineInfo($data[line]);
}
echo "</tr>\n";
echo "</table>\n";
?>
</form>
</div>
<div id=waiting><img src=http://ozs.kr/map/img/wait.gif></div>
<div id="map_canvas" style="width:100%; height:800px"></div>
<div id="display"></div>
<div id="infoWin"></div>
<div id="infoWin2"></div>
<div id="minAPNumDiv">최소 AP 검출 지역 : 
<select name=minAPNum id=minAPNum onChange='changeMinAPNum()'>
<?
for($i = 1; $i < 30; $i++)
	echo "<option value=$i>$i 개</option>\n";
?>
</select>
<button id=refreshbtn onclick=changeRefreshState()>자동</button>
<!--
<button onclick=addClick()>click event등록</button>
<button onclick=removeClick()>click event제거</button>
-->
<div id=totalNum></div>
</body>
</html>

<?
$db->close();
?>
				
